package com.service.impl;

import com.entity.pojo.WrScheduling;
import com.entity.pojo.WrTeamScheduling;
import com.entity.vo.SchedulingVo;
import com.mapper.WrSchedulingMapper;
import com.mapper.WrTeamSchedulingMapper;
import com.service.SchedulingService;
import lombok.extern.log4j.Log4j2;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

/**
 * @author qiuchi
 */
@Log4j2
@Service("schedulingService")
public class SchedulingServiceImpl implements SchedulingService {

    @Resource
    private WrSchedulingMapper schedulingMapper;

    @Resource
    private WrTeamSchedulingMapper teamSchedulingMapper;

    @Override
    public int insertScheduling(SchedulingVo schedulingVo) {
        WrScheduling scheduling = new WrScheduling()
                .setSchedulingName(schedulingVo.getSchedulingName())
                .setEndDateTime(schedulingVo.getEndDateTime())
                .setStartDateTime(schedulingVo.getStartDateTime())
                .setCreateTime(LocalDateTime.now());
        int res = schedulingMapper.insert(scheduling);
        int total = 0;
        if (res > 0) {
            total = teamSchedulingMapper.insert(new WrTeamScheduling()
                    .setTeamId(schedulingVo.getTeamId())
                    .setSchedulingId(scheduling.getId())
                    .setCreateTime(LocalDateTime.now())
            );
        }
        return total;
    }

    @Override
    public int deleteScheduling() {
        return 0;
    }

    @Override
    public List<SchedulingVo> listScheduling() {
        return schedulingMapper.listScheduling();
    }

    /**
     * 根据username获取有关用户的排班计划
     *
     * @param username
     * @return
     */
    @Override
    public List<SchedulingVo> listUserScheduling(String username) {
        return schedulingMapper.listUserScheduling(username);
    }
}
